// API工具函数

// 处理API错误
export const handleApiError = (error: any): string => {
  if (error.response?.data?.message) {
    return error.response.data.message;
  }
  
  if (error.message) {
    return error.message;
  }
  
  return '网络错误，请稍后重试';
};

// Token管理
export const tokenStorage = {
  get: (): string | null => {
    return localStorage.getItem('token');
  },
  
  set: (token: string): void => {
    localStorage.setItem('token', token);
  },
  
  remove: (): void => {
    localStorage.removeItem('token');
  },
};

// 用户信息管理
export const userStorage = {
  get: (): any | null => {
    const userStr = localStorage.getItem('user');
    return userStr ? JSON.parse(userStr) : null;
  },
  
  set: (user: any): void => {
    localStorage.setItem('user', JSON.stringify(user));
  },
  
  remove: (): void => {
    localStorage.removeItem('user');
  },
};

// 检查是否已登录
export const isAuthenticated = (): boolean => {
  return !!tokenStorage.get();
};

// 登出
export const logout = (): void => {
  tokenStorage.remove();
  userStorage.remove();
}; 